﻿using Aliyun.OSS;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace store.cyt369.com.dal
{
    public class StoreCategoryService:store.cyt369.com.dal.DbContexts.CategoryBaseService<StoreCategoryService>
    {
        /// <summary>
        /// 类别获取通过id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public store.cyt369.com.models.StoreCategory StoreCategorysGetByID(int? id)
        {
            using (var ctx = this.StoreCtx)
            {
                var categorys = ctx.StoreCategories
                    .Where(c => c.Id == id)
                    .FirstOrDefault();

                return categorys;
            }
        }

        /// <summary>
        /// 获取类别(包含所有子级) 通过id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public store.cyt369.com.models.StoreCategory StoreCategorysGetByParentID_InclueChilds(int id)
        {
            using (var ctx = this.StoreCtx)
            {
                var categorys = ctx.StoreCategories.Include(c => c.Childs)
                    .Where(c => c.Id == id)
                    .FirstOrDefault();
                return categorys;
            }
        }

        /// <summary>
        /// 获取子类别 通过父级ID
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IQueryable<store.cyt369.com.models.StoreCategory> StoreCategorysGetByParentID(int id)
        {
            var categorys = base.Get<store.cyt369.com.models.StoreCategory>().Where(c => c.ParentID == id);
            return categorys.AsNoTracking();
        }
    }
}
